//add by yuanfei
#include "CCLuaEngine.h"
#include "tolua++.h"
#include "tolua_fix.h"
#include "CCControlButton.h"
#include "CCScrollView.h"
#include "LuaCCBReader.h"

ButtonHandler* ButtonHandler::pSharedButtonHandler = NULL;

ButtonHandler* ButtonHandler::sharedButtonHandler()
{
    if (! pSharedButtonHandler)
    {
        pSharedButtonHandler= new ButtonHandler();
    }

    return pSharedButtonHandler;
}

ButtonHandler::ButtonHandler(){
}
void ButtonHandler::handle(CCObject* pSender, CCControlEvent event){
  //CCLog("handle %p %p %d", pSender, this, names_.size());
  CCLog("handle %s %p %d", names_[pSender].c_str(), this, event);
  CCLuaEngine* pEngine = CCLuaEngine::defaultEngine();
  pEngine->executeHandleFunction(names_[pSender].c_str(), pSender, event);
}
void ButtonHandler::addName(const char* name, CCObject* sender){
  CCLog("addName %d", names_.size());
  names_[(void*)sender] = name;
}


MySelectorResolver::~MySelectorResolver(){};
SEL_CCControlHandler MySelectorResolver::onResolveCCBCCControlSelector (CCObject *pTarget, CCString *pSelectorName){
  CCLog("MySelectorResolver %p", pTarget);
  CCLog("MySelectorResolver %s", pSelectorName->getCString());
  ButtonHandler::sharedButtonHandler()->addName(pSelectorName->getCString(), pTarget);
  return (SEL_CCControlHandler)&ButtonHandler::handle;
}
SEL_MenuHandler MySelectorResolver::onResolveCCBCCMenuItemSelector (CCObject *pTarget, CCString *pSelectorName){
}



int tolua_Cocos2d_CCBReader_load(lua_State* tolua_S)
{
  const char* pszFileName = ((const char*)  tolua_tostring(tolua_S,2,0));
  {
    MySelectorResolver mysr;
    CCLog("tolua_Cocos2d_CCBReader_load --> %s", pszFileName);
    CCNodeLoaderLibrary * ccNodeLoaderLibrary = CCNodeLoaderLibrary::newDefaultCCNodeLoaderLibrary();
    CCBReader * ccbReader = new cocos2d::extension::CCBReader(ccNodeLoaderLibrary, NULL, &mysr, NULL);
    ccbReader->autorelease();

    //ButtonHandler *handler = new ButtonHandler();
    CCNode *node = ccbReader->readNodeGraphFromFile(pszFileName, ButtonHandler::sharedButtonHandler());
    CCLog("load %p", node);
    CCLog("load handler %p", ButtonHandler::sharedButtonHandler());

    CCNode* tolua_ret = node;
    int nID = (tolua_ret) ? (int)tolua_ret->m_uID : -1;
    int* pLuaID = (tolua_ret) ? &tolua_ret->m_nLuaID : NULL;
    toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"CCLayer");
  }
  return 1;
}

int tolua_Cocos2d_CCButton_createWithFrameName(lua_State* tolua_S){
  const char* callback = ((const char*)  tolua_tostring(tolua_S,2,0));
  const char* normal = ((const char*)  tolua_tostring(tolua_S,3,0));
  const char* highlight = ((const char*)  tolua_tostring(tolua_S,4,0));
  const char* disabled = ((const char*)  tolua_tostring(tolua_S,5,0));
  {
    

    CCControlButton * button = CCControlButton::create();
    CCSpriteFrame *frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(normal);
    button->setBackgroundSpriteFrameForState(frame, CCControlStateNormal); 
    
    frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(highlight);
    button->setBackgroundSpriteFrameForState(frame, CCControlStateNormal); 

    frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(disabled);
    button->setBackgroundSpriteFrameForState(frame, CCControlStateNormal);

    ((CCControl *)button)->addTargetWithActionForControlEvents(
      ButtonHandler::sharedButtonHandler(), (SEL_CCControlHandler)&ButtonHandler::handle, CCControlEventTouchUpInside);
    ButtonHandler::sharedButtonHandler()->addName(callback, button);    
    
    button->setPreferredSize(frame->getOriginalSize());
    
    CCLog("tolua_Cocos2d_CCButton_create %s %s", callback, normal); 
    CCControlButton* tolua_ret = button;
    int nID = (tolua_ret) ? (int)tolua_ret->m_uID : -1;
    int* pLuaID = (tolua_ret) ? &tolua_ret->m_nLuaID : NULL;
    toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"CCButton");
  }
  return 1;
}

int tolua_Cocos2d_CCButton_createWithFileName(lua_State* tolua_S){
  const char* callback = ((const char*)  tolua_tostring(tolua_S,2,0));
  const char* normal = ((const char*)  tolua_tostring(tolua_S,3,0));
  const char* highlight = ((const char*)  tolua_tostring(tolua_S,4,0));
  const char* disabled = ((const char*)  tolua_tostring(tolua_S,5,0));
  {
    CCControlButton * button = CCControlButton::create();
    CCSpriteFrame *frame = NULL;
    {
      CCImage img;
      img.initWithImageFile(normal);
      frame = CCSpriteFrame::create(normal, CCRectMake(0,0,img.getWidth(),img.getHeight()));
      button->setBackgroundSpriteFrameForState(frame, CCControlStateNormal); 
    }
    { 
      CCImage img;
      img.initWithImageFile(normal);
      frame = CCSpriteFrame::create(highlight, CCRectMake(0,0,img.getWidth(),img.getHeight()));
      button->setBackgroundSpriteFrameForState(frame, CCControlStateNormal); 
    }
    {
      CCImage img;
      img.initWithImageFile(normal);
      frame = CCSpriteFrame::create(disabled, CCRectMake(0,0,img.getWidth(),img.getHeight()));
      button->setBackgroundSpriteFrameForState(frame, CCControlStateNormal);
    }
  
    ((CCControl *)button)->addTargetWithActionForControlEvents(
      ButtonHandler::sharedButtonHandler(), (SEL_CCControlHandler)&ButtonHandler::handle, 
      CCControlEventTouchUpInside|CCControlEventTouchDragOutside|CCControlEventTouchDragEnter|CCControlEventTouchDragInside
      |CCControlEventTouchDragExit);
      //CCControlEventTouchUpInside);
    ButtonHandler::sharedButtonHandler()->addName(callback, button);    
    
    button->setPreferredSize(frame->getOriginalSize());
    
    CCLog("tolua_Cocos2d_CCButton_create %s %s", callback, normal); 
    CCControlButton* tolua_ret = button;
    int nID = (tolua_ret) ? (int)tolua_ret->m_uID : -1;
    int* pLuaID = (tolua_ret) ? &tolua_ret->m_nLuaID : NULL;
    toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"CCButton");
  }
  return 1;
}

int tolua_Cocos2d_CCScrollView_setContainer(lua_State* tolua_S){
  CCScrollView* self = (CCScrollView*)  tolua_tousertype(tolua_S,1,0);
  CCNode* container = ((CCNode*)  tolua_tousertype(tolua_S,2,0));
  {
    self->setContainer(container); 
  }
  return 1;
}

int tolua_Cocos2d_CCScrollView_getContainer(lua_State* tolua_S){
  CCScrollView* self = (CCScrollView*)  tolua_tousertype(tolua_S,1,0);
  {
    CCNode* container = self->getContainer(); 
    CCNode* tolua_ret = container;
    int nID = (tolua_ret) ? (int)tolua_ret->m_uID : -1;
    int* pLuaID = (tolua_ret) ? &tolua_ret->m_nLuaID : NULL;
    toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"CCLayer");
  }
  return 1;
}

int tolua_Cocos2d_CCScrollView_getViewSize(lua_State* tolua_S){
  CCScrollView* self = (CCScrollView*)  tolua_tousertype(tolua_S,1,0);
  {
    CCSize size = self->getViewSize(); 
    CCSize tolua_ret = size; 
#ifdef __cplusplus
    void* tolua_obj = Mtolua_new((CCSize)(tolua_ret));
     tolua_pushusertype(tolua_S,tolua_obj,"CCSize");
    tolua_register_gc(tolua_S,lua_gettop(tolua_S));
#else
    void* tolua_obj = tolua_copy(tolua_S,(void*)&tolua_ret,sizeof(CCSize));
     tolua_pushusertype(tolua_S,tolua_obj,"CCSize");
    tolua_register_gc(tolua_S,lua_gettop(tolua_S));
#endif
  }
  return 1;
}
